package com.itheima;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class CookieUtils {
    //String cookieNameHistory="history"
    public static Cookie getCookies(HttpServletRequest request, String cookieNameHistory) {
        //1.获取浏览器所有的cookie
        Cookie[] cookies = request.getCookies();
        //防止空指针异常我们这里需要对数组cookies判断
        if (cookies != null && cookies.length > 0) {
            //2.遍历数组取出每个cookie
            for (Cookie cookie : cookies) {
                //3.取出cookie的name值
                String name = cookie.getName();
                //4.判断name是否等于cookieNameHistory即history
                if (cookieNameHistory.equals(name)) {
                    //5.说明含有name是history的cookie，返回cookie
                    return cookie;
                }
            }
        }

        //能够执行到这里说明浏览器没有name是history的cookie
        return null;
    }
}
